
package com.csc440.team2.intentRecievers;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;


import com.csc440.team2.communication.ClientHandler;
import com.csc440.team2.communication.MessageAction;


public class SMSReciever extends BroadcastReceiver
{

    @Override
    public void onReceive(Context context, Intent intent)
    {
        Thread handlerThread;
        Bundle extras = intent.getExtras();
        String message;
        String source;
        SmsMessage[] messages;
        if (extras != null)
        {
            Object[] pdus = (Object[]) extras.get("pdus");
            messages = new SmsMessage[pdus.length];
            for (int index = 0; index < messages.length; index++)
            {
                messages[index] = SmsMessage.createFromPdu((byte[]) pdus[index]);
                source = messages[index].getOriginatingAddress();
                message = messages[index].getMessageBody().toString();

                ClientHandler handler = new ClientHandler();
                handler.setAction(MessageAction.RECIEVE);
                handler.setKey(message);
                handler.setPort(23432);
                handler.setSender(source);
                handler.setContext(context);
                handlerThread = new Thread(handler);
                handlerThread.start();
            }
        }
    }

}
